package siec;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import siec.Connection;

public class Server {
	private int port;
        
        public Zegar zegarSerwer;
                
        public PrintWriter zapis = null;

	public ServerSocket serverSocket;

	private LinkedList<Connection> connections;

	public ConnectionListener connectionListener;

	private WaitForClients waitForClients;

	private boolean isRunning;

	public Server(int port) {
		this.port = port;
		isRunning = false;
	}

	public boolean start() {

        GregorianCalendar data=new GregorianCalendar();
        String nazwaPliku = "Serwer_log_"+data.get(Calendar.YEAR)+"_"+data.get(Calendar.MONTH)+"_"+data.get(Calendar.DAY_OF_MONTH)+"_"+data.get(Calendar.HOUR)+"_"+data.get(Calendar.MINUTE)+"_"+data.get(Calendar.SECOND)+".txt";
        zegarSerwer = new Zegar(0);
        try {
            zapis = new PrintWriter(new FileWriter(nazwaPliku, true));
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }  
        data=new GregorianCalendar();   
        zapis.println("Data i czas: "+data.get(Calendar.DAY_OF_MONTH)+"."+data.get(Calendar.MONTH)+"."+data.get(Calendar.YEAR)+" godz: "+data.get(Calendar.HOUR)+":"+data.get(Calendar.MINUTE)+":"+data.get(Calendar.SECOND));            
        zapis.println();   
            
            
		try {
			serverSocket = new ServerSocket(port);
		} catch (Exception ex) {
			return false;
		}

		isRunning = true;

		connections = new LinkedList<Connection>();
		connectionListener = new ConnectionListener(this);
		connectionListener.start();
		waitForClients = new WaitForClients(this);
		waitForClients.start();

		return true;
	}

	public boolean isRunning() {
		return isRunning;
	}

	public void stop() {
		isRunning = false;
                
                zapis.println("Zegar: " + zegarSerwer.toString());
                zapis.println();
            
                                                                zegarSerwer.increment();
                                                                zapis.println("Zegar: " + zegarSerwer.getLocalMax());
                                                                zapis.println("QUIT_SERVER");     
                                                                zapis.println();                
                zapis.close();  
                // przerwanie działanią wątku
		connectionListener.interrupt();
		waitForClients.interrupt();

		try {
			for (int i = 0; i < connections.size(); i++) {
				Connection connection = (Connection) connections.get(i);
				connection.close();
			}
			connections.clear();
			serverSocket.close();
		} catch (IOException ex) {
		}
	}

	public LinkedList<Connection> getConnections() {
		return connections;
	}
	
	public int getConnectionsCount() {
		return connections.size();
	}
}
